package com.ssr.master.common.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.ssr.master.common.druid.DataSourceNames;
import com.ssr.master.common.druid.DynamicDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 *
 *  配置多数据源
 *
 * @author ZhangPeng
 * @Describe //todo
 * @date
 * @updateBy
 * @other
 **/
@Configuration
public class DynamicDataSourceConfig {

    /**
     *
     * 创建 DataSource Bean
     *
     * @Describe //todo
     * @author ZhangPeng
     * @date 2020/6/10 15:38
     * @
     * @param
     * @return javax.sql.DataSource
     * @other
    **/
    @Bean
    @ConfigurationProperties("spring.datasource.druid.one")
    public DataSource oneDataSource(){
        DataSource dataSource = DruidDataSourceBuilder.create().build();
        return dataSource;
    }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.two")
    public DataSource twoDataSource(){
        DataSource dataSource = DruidDataSourceBuilder.create().build();
        return dataSource;
    }

    /**
     *
     *如果还有数据源,在这继续添加 DataSource Bean
     *
     * @Describe //todo
     * @author ZhangPeng
     * @date 2020/6/10 15:38
     * @
     * @param oneDataSource
     * @param twoDataSource
     * @return com.ssr.master.common.druid.DynamicDataSource
     * @other
    **/

    @Bean
    @Primary
    public DynamicDataSource dataSource(DataSource oneDataSource, DataSource twoDataSource) {
        Map<Object, Object> targetDataSources = new HashMap<Object, Object>(2);
        targetDataSources.put(DataSourceNames.ONE, oneDataSource);
        targetDataSources.put(DataSourceNames.TWO, twoDataSource);
        // 还有数据源,在targetDataSources中继续添加
        System.out.println("DataSources:" + targetDataSources);
        return new DynamicDataSource(oneDataSource, targetDataSources);
    }
}
